//
//  PrizeClawVideoCallbackHandler.cpp
//  MyApp
//
//  Created by Don on 2017/8/22.
//
//
#ifdef __cplusplus
extern "C" {
#endif
    
    
#include "PrizeClawVideoHandler.hpp"
#include "videodecode.h"
#include "glrenderer.h"
static PrizeClawVideoHandler* pcvInstance;
    
PrizeClawVideoHandler* PrizeClawVideoHandler::getInstance() {

    if (pcvInstance == NULL) {
        pcvInstance = new PrizeClawVideoHandler();
        videodecode_init();
    }
    
    return pcvInstance;

}

PrizeClawVideoHandler::PrizeClawVideoHandler() {
}

PrizeClawVideoHandler::~PrizeClawVideoHandler() {
    videodecode_stop();
    videodecode_uninit();
}

void PrizeClawVideoHandler::starPrizeClawVideo(const char *rtmp_ip, const char *id, int channel, int width, int height) {
    
    videodecode_start(rtmp_ip, id, channel, width, height, notifyFrameAvailable, notifyError);
}
    
void PrizeClawVideoHandler::setCameraChannel(int channel) {
    videodecode_toggle(channel);
}
    
//CGSize PrizeClawVideoHandler::getVideoSize() {
//    int video_width, video_height;
//    videodecode_getvideosize(&video_width, &video_height);
//    return CGSizeMake(video_width, video_height);
//}
    

// 注册回调函数的实现
void PrizeClawVideoHandler::registerFrameAvailableCallBackFunction(void * ref, FrameAvailableMessageHandler handler)
{
    this->ref = ref;
    this->frameAvailableHandler = handler;
    
}
void PrizeClawVideoHandler::registerFrameErrorCallBackFunction(void *ref, FrameErrorMessageHandler handler) {
    this->ref = ref;
    this->frameErrorHandler = handler;
}

#pragma mark - prize claw video callback
void PrizeClawVideoHandler::notifyFrameAvailable() {
//    printf("video frame available");
    uint8_t* frame = videodecode_getframe();
    if (pcvInstance->frameAvailableHandler != NULL && frame) {
        pcvInstance->frameAvailableHandler(pcvInstance->ref,frame);
    }
    videodecode_releaseframe();
}

void PrizeClawVideoHandler::notifyError(int error) {
    printf("video frame error ..");
    
    if (pcvInstance->frameErrorHandler != NULL) {
        pcvInstance->frameErrorHandler(pcvInstance->ref,error);
    }
}
    
#ifdef __cplusplus
}
#endif

